package com.yuanli.cosmeticmirror.persenter;

import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.lifecycle.LifecycleOwner;

import com.comenjoysoft.baselibrary.listener.CallbackListener;
import com.comenjoysoft.baselibrary.presenter.BasePresenter;
import com.comenjoysoft.baselibrary.util.EncryptUtils;
import com.comenjoysoft.baselibrary.util.SPUtils;
import com.mob.MobSDK;
import com.orhanobut.logger.Logger;
import com.yuanli.cosmeticmirror.base.Constant;
import com.yuanli.cosmeticmirror.base.MyApp;
import com.yuanli.cosmeticmirror.bean.LoginBean;
import com.yuanli.cosmeticmirror.model.biz.ILoginBiz;
import com.yuanli.cosmeticmirror.model.biz.IRegisterBiz;
import com.yuanli.cosmeticmirror.model.impl.LoginImpl;
import com.yuanli.cosmeticmirror.model.impl.RegisterImpl;
import com.yuanli.cosmeticmirror.model.iview.ILoginView;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import me.jessyan.autosize.utils.LogUtils;

import static com.shuyu.gsyvideoplayer.GSYVideoBaseManager.TAG;
import static com.yuanli.cosmeticmirror.base.MyApp.isLogin;

public class LoginPersenter extends BasePresenter<ILoginView> {
    private final ILoginBiz biz;
    private final IRegisterBiz registerBiz;

    public LoginPersenter(ILoginView view, LifecycleOwner owner) {
        attachView(view);
        biz = new LoginImpl(owner);
        registerBiz = new RegisterImpl(owner);
    }

    public void login(String phone, String password) {
        biz.login(phone, password, new CallbackListener<LoginBean>() {
            @Override
            public void success(LoginBean object) {
                if (object != null){
                    isLogin = true;
                    SPUtils.getInstance().put(Constant.SP_USER_ACCOUNT, phone);
                    SPUtils.getInstance().put(Constant.SP_USER_PASSWORD, password);
                    SPUtils.getInstance().put(Constant.IS_FIRST_LOGIN, true);
                    SPUtils.getInstance().put(Constant.SP_USER_NAME, object.getUser_Name());
                    if (!TextUtils.isEmpty(object.getImgUrl())) {
                        SPUtils.getInstance().put(Constant.SP_USER_PHOTO_NET, object.getImgUrl());
                    }
                    mView.loginSucess(object);
                    return;
                }
                showToast("操作失败");
            }

            @Override
            public void failed(Object object) {
                isLogin = false;
                mView.error();
            }
        });
    }

    private void showToast(String str){
        if (mView != null){
            Toast.makeText(mView.getActivity(), str, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 第三方登录
     *
     * @param name
     */
    public void otherLogin(String name) {
        MobSDK.submitPolicyGrantResult(true, null);
        Logger.i(TAG, "otherLogin: 第三方登录");
        mView.showToast("正在打开第三方应用");
        Platform plat = ShareSDK.getPlatform(name);
        plat.removeAccount(true);
        plat.SSOSetting(false);
        plat.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                SPUtils.getInstance().put(Constant.SP_USER_PHOTO_NET, platform.getDb().getUserIcon());
                Logger.i("otherLogin onComplete: " + platform.getDb().getUserId() + "," + platform.getDb().getUserName() + "," + platform.getDb().getUserIcon());
                mView.otherLoginSucess(platform);

            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                //                Looper.prepare();
                //                Toast.makeText(mRootView.getActivity(), "登录失败，请重试", Toast.LENGTH_SHORT).show();
                Logger.i("otherLogin onError: " + throwable.getMessage());
                //                Looper.loop();
                mView.otherLoginError();
            }

            @Override
            public void onCancel(Platform platform, int i) {
                //                Looper.prepare();
                //                Toast.makeText(mRootView.getActivity(), "取消登录", Toast.LENGTH_SHORT).show();
                //                Looper.loop();
                mView.showToast("取消登录");
            }
        });
        ShareSDK.setActivity(mView.getActivity());
        plat.showUser(null);
    }

    public void register(String userName, String phone, String password) {
        registerBiz.register(userName, phone, password, new CallbackListener<String>() {
            @Override
            public void success(String object) {
                SPUtils.getInstance().put(Constant.SP_USER_ACCOUNT, phone);
                if (object.contains("SUCCESS")) {
                    mView.register();
                } else if (object.contains("Account Exist")) {
                    mView.register();
                } else {
                    mView.error();
                }
            }

            @Override
            public void failed(Object object) {
                mView.error();
            }
        });
    }

}
